package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.badge.BadgeUtils;

import cn.lrwjz.androidguide.R;

/**
 * Badge示例Activity
 * 展示Material Design徽章的使用方法
 */
public class BadgeActivity extends AppCompatActivity {
    private BadgeDrawable basicBadge;
    private BadgeDrawable numberBadge;
    private BadgeDrawable largeNumberBadge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_badge);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Badge 示例");
        }

        // 设置基础徽章
        View basicTarget = findViewById(R.id.basicBadgeTarget);
        basicBadge = BadgeDrawable.create(this);
        basicBadge.setBackgroundColor(getResources().getColor(R.color.purple_500, null));
        basicBadge.setBadgeGravity(BadgeDrawable.TOP_END);
        BadgeUtils.attachBadgeDrawable(basicBadge, basicTarget);

        // 设置数字徽章
        View numberTarget = findViewById(R.id.numberBadgeTarget);
        numberBadge = BadgeDrawable.create(this);
        numberBadge.setNumber(5);
        numberBadge.setBackgroundColor(getResources().getColor(R.color.teal_200, null));
        numberBadge.setBadgeGravity(BadgeDrawable.TOP_END);
        BadgeUtils.attachBadgeDrawable(numberBadge, numberTarget);

        // 设置大数字徽章
        View largeNumberTarget = findViewById(R.id.largeNumberBadgeTarget);
        largeNumberBadge = BadgeDrawable.create(this);
        largeNumberBadge.setNumber(100);
        largeNumberBadge.setMaxCharacterCount(3);
        largeNumberBadge.setBackgroundColor(getResources().getColor(R.color.purple_200, null));
        largeNumberBadge.setBadgeGravity(BadgeDrawable.TOP_END);
        BadgeUtils.attachBadgeDrawable(largeNumberBadge, largeNumberTarget);

        // 设置点击事件
        basicTarget.setOnClickListener(v -> {
            basicBadge.setVisible(!basicBadge.isVisible());
        });

        numberTarget.setOnClickListener(v -> {
            int currentNumber = numberBadge.getNumber();
            numberBadge.setNumber(currentNumber + 1);
        });

        largeNumberTarget.setOnClickListener(v -> {
            int currentNumber = largeNumberBadge.getNumber();
            largeNumberBadge.setNumber(currentNumber + 10);
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 